/*
 *所有关于Plan_Summary_Three_Location类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Plan_Summary_Three_LocationService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using DGZImp.Core.BaseProvider;
using DGZImp.Core.Extensions.AutofacManager;
using ATkg.BizAppProject.DomainModels;
using System.Linq;
using DGZImp.Core.Utilities;
using System.Linq.Expressions;
using DGZImp.Core.Extensions;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using ATkg.BizAppProject.IRepositories;
using DGZImp.Core.DbSqlSugar;

namespace ATkg.BizAppProject.Services
{
    public partial class Plan_Summary_Three_LocationService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IPlan_Summary_Three_LocationRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public Plan_Summary_Three_LocationService(
            IPlan_Summary_Three_LocationRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public async Task<object> DealDataFromLocation()
        {

            var res = await Db.Set<Plan_Summary_Single_Location>().ToListAsync();
            var existingThreeLocations = await Db.Set<Plan_Summary_Three_Location>().ToListAsync();

            var resultList = res
                .GroupBy(x => x.ProductId)
                .Select(g => {
                    var productId = g.Key;

                    // 从现有数据中获取这6个字段的值
                    var existingData = existingThreeLocations.FirstOrDefault(x => x.ProductId == productId);

                    return new Plan_Summary_Three_Location
                    {
                        ProductId = productId,

                        // 保留的6个字段
                        RequirementIn4Week = existingData?.RequirementIn4Week ?? 0,
                        RequirementIn5To9Week = existingData?.RequirementIn5To9Week ?? 0,
                        InventoryMaterial = existingData?.InventoryMaterial ?? 0,
                        InventoryBlank = existingData?.InventoryBlank ?? 0,
                        InventoryProcessCheck = existingData?.InventoryProcessCheck ?? 0,
                        InventoryFinal = existingData?.InventoryFinal ?? 0,

                        // 需要累加的字段
                        UnfinishedFinalOEM = g.Where(x => x.Type == "成品").Sum(x => x.OrderUnfinish ?? 0),
                        UnfinishedBlankOEM = g.Where(x => x.Type == "毛坯").Sum(x => x.OrderUnfinish ?? 0),

                        BlankPlan34Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan34Week ?? 0),
                        BlankPlan35Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan35Week ?? 0),
                        BlankPlan36Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan36Week ?? 0),
                        BlankPlan37Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan37Week ?? 0),
                        BlankPlan38Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan38Week ?? 0),
                        BlankPlan39Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan39Week ?? 0),
                        BlankPlan40Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan40Week ?? 0),
                        BlankPlan41Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan41Week ?? 0),
                        BlankPlan42Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan42Week ?? 0),
                        BlankPlan43Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan43Week ?? 0),
                        BlankPlan44Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan44Week ?? 0),
                        BlankPlan45Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan45Week ?? 0),
                        BlankPlan46Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan46Week ?? 0),
                        BlankPlan47Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan47Week ?? 0),
                        BlankPlan48Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan48Week ?? 0),
                        BlankPlan49Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan49Week ?? 0),
                        BlankPlan50Week = g.Where(x => x.Type == "毛坯").Sum(x => x.Plan50Week ?? 0),
                        //正确拼写应该是Final，数据库那里拼错了，为了不折腾，暂时不改
                        FianlPlan34Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan34Week ?? 0),
                        FianlPlan35Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan35Week ?? 0),
                        FianlPlan36Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan36Week ?? 0),
                        FianlPlan37Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan37Week ?? 0),
                        FianlPlan38Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan38Week ?? 0),
                        FianlPlan39Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan39Week ?? 0),
                        FianlPlan40Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan40Week ?? 0),
                        FianlPlan41Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan41Week ?? 0),
                        FianlPlan42Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan42Week ?? 0),
                        FianlPlan43Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan43Week ?? 0),
                        FianlPlan44Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan44Week ?? 0),
                        FianlPlan45Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan45Week ?? 0),
                        FianlPlan46Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan46Week ?? 0),
                        FianlPlan47Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan47Week ?? 0),
                        FianlPlan48Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan48Week ?? 0),
                        FianlPlan49Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan49Week ?? 0),
                        FianlPlan50Week = g.Where(x => x.Type == "成品").Sum(x => x.Plan50Week ?? 0),
                    };
                })
                .ToList();

            await Db.DoDeleteAsync<Plan_Summary_Three_Location>(x => x.IsValid >= 0, true);
            await Db.DoInsertAsync<Plan_Summary_Three_Location>(resultList);

            var summaryData = await Db.Set<Plan_Summary_Three_Location>()
                    .Where(x => x.IsValid == 1) // 汇总表按 ProductId 唯一分组，此条件可定位单条
                    .FirstOrDefaultAsync();
            return summaryData == null ? "暂无数据" : summaryData.CreateTime.ToString();
        }

        public async Task<object> GetDateFromDb()
        {

            var summaryData = await Db.Set<Plan_Summary_Three_Location>()
                    .Where(x => x.IsValid == 1) // 汇总表按 ProductId 唯一分组，此条件可定位单条
                    .FirstOrDefaultAsync();
            return summaryData == null ? "暂无数据" : summaryData.CreateTime.ToString();
        }
    }
}
